package com.neusoft.databus.policy.fixture;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import com.neusoft.databus.common.constant.DatabusErrorCode;
import com.neusoft.databus.policy.enums.PolicyType;
import com.neusoft.databus.policy.model.Policy;
import com.neusoft.databus.policy.repository.PolicyRepository;

/**
 * 策略数据初始化
 *
 * @author naxiang
 */
@Component
@Profile({"fixture", "prod" })
public class PolicyFixture implements CommandLineRunner {
    @Autowired
    private PolicyRepository policyRepository;

    @Override
    public void run(final String... args) throws Exception {
        this.policyFixture();
    }

    private void policyFixture() {
        //DatabusErrorCode
        Policy policy = this.policyRepository.findByCode(DatabusErrorCode.ERR_CODE_406001);
        if (policy == null) {
            policy = this.getPolicy(DatabusErrorCode.ERR_CODE_406001, "无法与网关建立连接", "无法与网关建立连接",
                    "{\"0\":\"无法与网关建立连接!\"}", PolicyType.GATEWAY_DISCONNECT);
            this.policyRepository.save(policy);
        }
        policy = this.policyRepository.findByCode(DatabusErrorCode.ERR_CODE_406003);
        if (policy == null) {
            policy = this.getPolicy(DatabusErrorCode.ERR_CODE_406003, "网关主动终止连接", "网关主动终止连接",
                    "{\"0\":\"网关主动终止连接!\"}", PolicyType.DEVICE_FAULT);
            this.policyRepository.save(policy);
        }
        policy = this.policyRepository.findByCode(DatabusErrorCode.ERR_CODE_406004);
        if (policy == null) {
            policy = this.getPolicy(DatabusErrorCode.ERR_CODE_406004, "未采集到任何数据", "未采集到任何数据",
                    "{\"0\":\"未采集到任何数据!\"}",
                    PolicyType.DEVICE_FAULT);
            this.policyRepository.save(policy);
        }

        policy = this.policyRepository.findByCode(DatabusErrorCode.ERR_CODE_406005);
        if (policy == null) {
            policy = this.getPolicy(DatabusErrorCode.ERR_CODE_406005, "数据采集丢失部分指标数据 ", "数据采集丢失部分指标数据 ",
                    "{\"0\":\"数据采集丢失部分指标数据 !\"}",
                    PolicyType.DEVICE_FAULT);
            this.policyRepository.save(policy);
        }
        policy = this.policyRepository.findByCode(DatabusErrorCode.ERR_CODE_400001);
        if (policy == null) {
            policy = this.getPolicy(DatabusErrorCode.ERR_CODE_400001, "未处理的原始异常", "未处理的原始异常", "{\"0\":\"未处理的原始异常!\"}",
                    PolicyType.SYSTEM);
            this.policyRepository.save(policy);
        }
    }

    private Policy getPolicy(final String code, final String name, final String description,
            final String contentTemplate, final PolicyType tyep) {
        final Policy policy = new Policy();
        policy.setCode(code);
        policy.setName(name);
        policy.setDescription(description);
        policy.setContentTemplate(contentTemplate);
        policy.setPolicyType(tyep);
        return policy;
    }
}
